package com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.fwsjcj.commen.util.Constant;
import com.css.zfzx.fwsjcj.modules.analysis.vo.AnalysisQueryVo;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.entity.FwVillageHouseEntity;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.entity.FwVillagePubhouseEntity;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.service.VillageHouseFormService;
import com.css.zfzx.fwsjcj.modules.taskmanage.taskfrom.service.VillagePubHouseFormService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/fw/task/villagePubHouse")
@Slf4j
public class VillagePubHouseFromController {
    @Autowired
    VillagePubHouseFormService villagePubHouseFormService;

    /***
     * 农村公共建筑
     * @param fwVillagePubhouseEntity
     * @return
     */
    @PostMapping("villagePubHouse")
    public RestResponse saveForm(FwVillagePubhouseEntity fwVillagePubhouseEntity, String type) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            villagePubHouseFormService.saveForm(fwVillagePubhouseEntity, type);
            jsonObject.put("message", "保存成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "保存失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查看农村公共住房详情
     *
     * @param formId
     * @return
     */
    @GetMapping("findVillagePubHouseById")
    public RestResponse findCityById(String formId) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            FwVillagePubhouseEntity cityById = villagePubHouseFormService.findVillagePubHouseById(formId);
            jsonObject.put("message", "查询成功");
            restResponse = RestResponse.succeed(cityById);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改表单
     *
     * @param type
     * @param fwVillagePubhouseEntity
     * @return
     */
    @PutMapping("{type}/updCity")
    public RestResponse updCity(@PathVariable String type, @RequestBody FwVillagePubhouseEntity fwVillagePubhouseEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            villagePubHouseFormService.updVillPub(type, fwVillagePubhouseEntity);
            jsonObject.put("message", "修改成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "修改成功";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /***
     * 农村住宅结构类型饼状图
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("villagePubStructure")
    public RestResponse cityStructure(AnalysisQueryVo analysisQueryVo) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            String[] array = new String[]{"砖（砌块）混", "低层框架", "轻钢（钢）装配式", "混凝土装配式", "砖木", "土木", "石木", "木结构", "窑洞", "混杂结构(砖/砌块/土/石)", "其它"};
            //查询审核通过的数据
            analysisQueryVo.setSpType(Constant.SP_YES);
            // 建筑数量
            ArrayList<Map> resultCount = new ArrayList<>();
            for (String structureType : array) {
                // 结构类型查询参数变化
                analysisQueryVo.setStructureType(structureType);
                long count = villagePubHouseFormService.count(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultCount.add(map);
            }
            //建筑面积
            ArrayList<Map> resultArea = new ArrayList<>();
            for (String structureType : array) {
                // 结构类型查询参数变化
                analysisQueryVo.setStructureType(structureType);
                long count = villagePubHouseFormService.areaCount(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultArea.add(map);
            }
            jsonObject.put("villageNum", resultCount);
            jsonObject.put("villageArea", resultCount);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /***
     * 农村公共建筑年代饼状图
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("cityYear")
    public RestResponse cityYear(AnalysisQueryVo analysisQueryVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] array = new String[]{"70s以前", "70s", "80s", "90s", "00s", "10s"};
            //查询审核通过的数据
            analysisQueryVo.setSpType(Constant.SP_YES);
            // 建筑数量
            ArrayList<Map> resultCount = new ArrayList<>();
            for (String structureType:array) {
                // 年代查询参数变化
                analysisQueryVo.setBuildingTime(structureType);
                long count = villagePubHouseFormService.count(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultCount.add(map);
            }
            //建筑面积
            ArrayList<Map> resultArea = new ArrayList<>();
            for (String structureType:array) {
                // 年代查询参数变化
                analysisQueryVo.setBuildingTime(structureType);
                long count = villagePubHouseFormService.areaCount(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultArea.add(map);
            }
            jsonObject.put("villagePubNum", resultCount);
            jsonObject.put("villagePubArea", resultArea);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /***
     * 农村公共建筑抗震设防烈度饼状图
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("villagePubFortificationIntensity")
    public RestResponse villagePubFortificationIntensity(AnalysisQueryVo analysisQueryVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] array = new String[]{"未设防", "6度", "7度", "8度", "9度", "10度"};
            //查询审核通过的数据
            analysisQueryVo.setSpType(Constant.SP_YES);
            // 建筑数量
            ArrayList<Map> resultCount = new ArrayList<>();
            for (String structureType:array) {
                // 震设防烈度查询参数变化
                analysisQueryVo.setNowFortificationIntensity(structureType);
                long count = villagePubHouseFormService.count(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultCount.add(map);
            }
            //建筑面积
            ArrayList<Map> resultArea = new ArrayList<>();
            for (String structureType:array) {
                // 震设防烈度查询参数变化
                analysisQueryVo.setNowFortificationIntensity(structureType);
                long count = villagePubHouseFormService.areaCount(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultArea.add(map);
            }
            jsonObject.put("villagePubNum", resultCount);
            jsonObject.put("villagePubArea", resultArea);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /***
     * 农村公共建筑抗震设防情况饼状图
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("villagePubFortificationResult")
    public RestResponse villagePubFortificationResult(AnalysisQueryVo analysisQueryVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] array = new String[]{"未抗震设防", "抗震设防能力不足", "基本符合抗震设防要求"};
            //查询审核通过的数据
            analysisQueryVo.setSpType(Constant.SP_YES);
            // 建筑数量
            ArrayList<Map> resultCount = new ArrayList<>();
            for (String structureType:array) {
                // 抗震设防情况查询参数变化
                analysisQueryVo.setResult(structureType);
                long count = villagePubHouseFormService.count(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultCount.add(map);
            }
            //建筑面积
            ArrayList<Map> resultArea = new ArrayList<>();
            for (String structureType:array) {
                // 抗震设防情况查询参数变化
                analysisQueryVo.setResult(structureType);
                long count = villagePubHouseFormService.areaCount(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultArea.add(map);
            }
            jsonObject.put("noCityNum", resultCount);
            jsonObject.put("noCityArea", resultArea);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    /***
     * 农村公共建筑安全隐患饼状图
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("villagePubSafeHidden")
    public RestResponse villagePubSafeHidden(AnalysisQueryVo analysisQueryVo){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try{
            String[] array = new String[]{"有", "无"};
            //查询审核通过的数据
            analysisQueryVo.setSpType(Constant.SP_YES);
            // 建筑数量
            ArrayList<Map> resultCount = new ArrayList<>();
            for (String structureType:array) {
                // 安全隐患查询参数变化
                analysisQueryVo.setIsDamage(structureType);
                long count = villagePubHouseFormService.count(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultCount.add(map);
            }
            //建筑面积
            ArrayList<Map> resultArea = new ArrayList<>();
            for (String structureType:array) {
                // 安全隐患查询参数变化
                analysisQueryVo.setIsDamage(structureType);
                long count = villagePubHouseFormService.areaCount(analysisQueryVo);
                HashMap<String, Object> map = new HashMap<>();
                map.put("value", count);
                map.put("name", structureType);
                resultArea.add(map);
            }
            jsonObject.put("villagePubNum", resultCount);
            jsonObject.put("villagePubArea", resultArea);
            jsonObject.put("message", "查询成功!");
            restResponse = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改
     * @param fwVillagePubhouseEntity
     * @return
     */
    @PostMapping("update")
    public RestResponse update(@RequestBody FwVillagePubhouseEntity fwVillagePubhouseEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            villagePubHouseFormService.update(fwVillagePubhouseEntity);
            jsonObject.put("message", "修改成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "修改失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
